package com.javaee.shortlink.admin.controller;

import com.javaee.shortlink.admin.common.convention.result.Result;
import com.javaee.shortlink.admin.common.convention.result.Results;
import com.javaee.shortlink.admin.dao.entity.GroupDO;
import com.javaee.shortlink.admin.dto.req.GroupSaveReqDTO;
import com.javaee.shortlink.admin.dto.req.GroupSortReqDTO;
import com.javaee.shortlink.admin.dto.req.GroupUpdateReqDTO;
import com.javaee.shortlink.admin.dto.resp.GroupRespDTO;
import com.javaee.shortlink.admin.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接分组控制层
 */
@RestController
@RequiredArgsConstructor
public class GroupController {
    private final GroupService groupService;

    /**
     *新增短链接分组
     */
    @PostMapping("api/short-link/admin/v1/group")
    public Result<Void> addGroup(@RequestBody GroupSaveReqDTO groupSaveReqDTO) {
        groupService.saveGroup(groupSaveReqDTO.getName());
        return Results.success();
    }

    /**
     *查询当前用户短链接分组
     */
    @GetMapping("api/short-link/admin/v1/group")
    public Result<List<GroupRespDTO>> listGroup() {
        return Results.success(groupService.listGroup());
    }

    /**
     *修改短链接分组
     */
    @PutMapping("api/short-link/admin/v1/group")
    public Result<Void> updateGroup(@RequestBody GroupUpdateReqDTO groupUpdateReqDTO) {
        groupService.updateGroup(groupUpdateReqDTO);
        return Results.success();
    }

    /**
     *删除短链接分组
     */
    @DeleteMapping("api/short-link/admin/v1/group")
    public Result<Void> deleteGroup(@RequestParam String gid) {
        groupService.deleteGroup(gid);
        return Results.success();
    }

    /**
     *排序短链接分组
     */
    @PostMapping("api/short-link/admin/v1/group/sort")
    public Result<Void> sortGroup(@RequestBody List<GroupSortReqDTO> groupSortReqDTOList) {
        groupService.sortGroup(groupSortReqDTOList);
        return Results.success();
    }
}
